package sg.edu.nus.iss.jass.gui;

import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.SwingConstants;

import sg.edu.nus.iss.jass.constant.CommonConstants;
import sg.edu.nus.iss.jass.controller.UserController;
import sg.edu.nus.iss.jass.exception.ValidationException;
import sg.edu.nus.iss.jass.gui.component.ValidateJTextField;
import sg.edu.nus.iss.jass.privilege.JobSeekerGuestRole;
import sg.edu.nus.iss.jass.privilege.JobSeekerRole;
import sg.edu.nus.iss.jass.privilege.AdvertiserRole;
import sg.edu.nus.iss.jass.privilege.User;
import sg.edu.nus.iss.jass.validation.FormValidator;
import sg.edu.nus.iss.jass.validation.ValidationRule;

public class LoginPanel extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 4975528804578330589L;


	private ValidateJTextField usernameTxt;
	private JPasswordField passwordTxt;
	private JLabel lblUsername;
	private JLabel lblPassword;

	private JButton btnLogin;
	private JButton btnGuestLogin;
	private UserController userController;

	private MainFrame frame;

	

	/**
	 * Create the panel.
	 */
	public LoginPanel(MainFrame mainFrame) {
		this.frame = mainFrame;
		this.userController = new UserController();
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] { 0, 0, 0, 0 };
		gridBagLayout.rowHeights = new int[] { 24, 24, 24, 24, 0 };
		gridBagLayout.columnWeights = new double[] { 0.0, 0.0, 1.0,
				Double.MIN_VALUE };
		gridBagLayout.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0,
				Double.MIN_VALUE };
		setLayout(gridBagLayout);

		lblUsername = new JLabel("User ID");
		lblUsername.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblUsername.setHorizontalAlignment(SwingConstants.RIGHT);
		GridBagConstraints gbc_lblUsername = new GridBagConstraints();
		gbc_lblUsername.anchor = GridBagConstraints.EAST;
		gbc_lblUsername.insets = new Insets(5, 5, 5, 5);
		gbc_lblUsername.gridx = 0;
		gbc_lblUsername.gridy = 0;
		add(lblUsername, gbc_lblUsername);

		this.usernameTxt = new ValidateJTextField("User ID",
				ValidationRule.NOT_NULL);
		this.usernameTxt.setColumns(10);
		GridBagConstraints gbc_usernameTxt = new GridBagConstraints();
		gbc_usernameTxt.fill = GridBagConstraints.HORIZONTAL;
		gbc_usernameTxt.insets = new Insets(5, 5, 5, 5);
		gbc_usernameTxt.gridx = 1;
		gbc_usernameTxt.gridy = 0;
		add(this.usernameTxt, gbc_usernameTxt);

		lblPassword = new JLabel("Password");
		lblPassword.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblPassword.setHorizontalAlignment(SwingConstants.RIGHT);
		GridBagConstraints gbc_lblPassword = new GridBagConstraints();
		gbc_lblPassword.anchor = GridBagConstraints.EAST;
		gbc_lblPassword.insets = new Insets(5, 5, 5, 5);
		gbc_lblPassword.gridx = 0;
		gbc_lblPassword.gridy = 1;
		add(lblPassword, gbc_lblPassword);

		this.passwordTxt = new JPasswordField();
		this.passwordTxt.setColumns(10);
		GridBagConstraints gbc_passwordTxt = new GridBagConstraints();
		gbc_passwordTxt.fill = GridBagConstraints.HORIZONTAL;
		gbc_passwordTxt.insets = new Insets(5, 5, 5, 5);
		gbc_passwordTxt.gridx = 1;
		gbc_passwordTxt.gridy = 1;
		add(this.passwordTxt, gbc_passwordTxt);

				
		
		

		btnLogin = new JButton("LOGIN");
		btnLogin.setFont(new Font("Tahoma", Font.BOLD, 11));
		btnLogin.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					String username = usernameTxt.getValidatedText();
					String password = new String(passwordTxt.getPassword());
					FormValidator.validate("Password",
							password, ValidationRule.NOT_NULL);
					User user = userController.getUserById(username);
					if (user != null) {
						if (password.equals(user.getPassword())) {
							if (user.getRoleId() != null && CommonConstants.ADVERTISER_ROLEID.equals(user.getRoleId())) {
								user.setRole(new AdvertiserRole());
							} else if (user.getRoleId() != null && CommonConstants.JOBSEEKER_ROLEID.equals(user.getRoleId())) {
								user.setRole(new JobSeekerRole());
							}
							frame.setUser(user);
						} else {
							JOptionPane.showMessageDialog(null, "Authentication Failed", "Error:", JOptionPane.ERROR_MESSAGE);
						}
					} else {
						JOptionPane.showMessageDialog(null, "Authentication Failed", "Error:", JOptionPane.ERROR_MESSAGE);
					}
					
				} catch (ValidationException ve) {
					JOptionPane.showMessageDialog(null, "Invalid username and password", "Error:", JOptionPane.ERROR_MESSAGE);
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}
		});
		GridBagConstraints gbc_btnLogin = new GridBagConstraints();
		gbc_btnLogin.fill = GridBagConstraints.HORIZONTAL;
		gbc_btnLogin.anchor = GridBagConstraints.NORTH;
		gbc_btnLogin.insets = new Insets(5, 5, 0, 5);
		gbc_btnLogin.gridx = 1;
		gbc_btnLogin.gridy = 2;
		add(btnLogin, gbc_btnLogin);
		
		
		btnGuestLogin = new JButton("GUEST LOGIN");
		btnGuestLogin.setFont(new Font("Tahoma", Font.BOLD, 11));
		btnGuestLogin.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				
				try {
					User guestUser = new User(null,null,new JobSeekerGuestRole());
					frame.setUser(guestUser);
				} catch (Exception ex) {
					//ErrorDialog.showUnhandledErrorMessageDialog(ex);
				}
			}
		});
		GridBagConstraints gbc_btnGuestLogin = new GridBagConstraints();
		gbc_btnGuestLogin.fill = GridBagConstraints.HORIZONTAL;
		gbc_btnGuestLogin.anchor = GridBagConstraints.NORTH;
		gbc_btnGuestLogin.insets = new Insets(5, 5, 0, 5);
		gbc_btnGuestLogin.gridx = 2;
		gbc_btnGuestLogin.gridy = 0;
		add(btnGuestLogin, gbc_btnGuestLogin);
	}
}